package com.smalls.newvideotwo.mvp.presenter;

import com.smalls.newvideotwo.db.Category;
import com.smalls.newvideotwo.mvp.base.BaseBeanResult;
import com.smalls.newvideotwo.mvp.base.BasePresenter;
import com.smalls.newvideotwo.mvp.bean.MovieDetail;
import com.smalls.newvideotwo.mvp.beanResult.GetHotMovieResult;
import com.smalls.newvideotwo.mvp.http.constants.PortalErr;
import com.smalls.newvideotwo.mvp.http.constants.PortalErrUtil;
import com.smalls.newvideotwo.mvp.model.GetMovieByCategoryModel;
import com.smalls.newvideotwo.mvp.view.IGetHotMovieView;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.utils.Constant;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

/**
 * Created by hs on 2018/3/20.
 */

public class GetMovieByCategoryPresenter extends BasePresenter<IGetHotMovieView> implements BaseBeanResult.IGetMovieByCategory {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "GetMovieByCategoryPresenter":"GetMovieByCategoryPresenter";
    private com.smalls.newvideotwo.mvp.model.modelInterface.IGetMovieByCategory model = null;
    private boolean isLive;
    private Category mCategoryList ;
    private int mGotSize = 0;
    private List<MovieDetail> data;
    private GetHotMovieResult mGetHotMovieResult;
    private int start = 0;
    private int end = 999;

    public GetMovieByCategoryPresenter(IGetHotMovieView view) {
        super(view);
        model = new GetMovieByCategoryModel();
        data = new ArrayList<MovieDetail>();
        mGetHotMovieResult = new GetHotMovieResult();
        mGetHotMovieResult.setData(data);
    }

    public void Abort() {
        if(model!=null) {
            model.Abort();
        }
    }

    public void request(boolean z,Category categoryList,int start,int end) {
        this.start = start;
        this.end = end;
        mGotSize = 0;
        mCategoryList = categoryList;
        if(model!=null&&mCategoryList!=null) {
            model.getMovie(this,mCategoryList.getCategory_id(),start,end);
        } else {
            ((IGetHotMovieView) this.view).showErrDialogGetHotMovie("");
        }
        isLive = z;
        if(mGetHotMovieResult.getData()!=null) {
            mGetHotMovieResult.getData().clear();
        }
    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        String errCode = "";
        if ((e instanceof TimeoutException) || (e instanceof ConnectException) || (e instanceof SocketTimeoutException)) {
            LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fEB2");
            errCode = PortalErr.STR_BOX_EPG_TIMEOUT_ERROR;
        } else {
            LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fEB3");
            errCode = PortalErr.PORTAL_SYSTEM;
        }
        PortalErrUtil.getInstance().setErrCode(errCode);
        ((IGetHotMovieView) this.view).showErrDialogGetHotMovie(errCode);
    }

    @Override
    public void onNext(GetHotMovieResult result) {
        PortalErrUtil.getInstance().setErrCode("");
        if (result == null
                || result.getData()==null
                ||result.getData().size() <= 0) {
            LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fEB1");
            PortalErrUtil.getInstance().setErrCode(PortalErr.PORTAL_ROOT_COLUMN);
            ((IGetHotMovieView) this.view).showErrDialogGetHotMovie(PortalErr.PORTAL_ROOT_COLUMN);
        } else {
            LogUtil.d(this.TAG, this.TAG + "  onNext mGotSize=" + mGotSize);
            mGetHotMovieResult.getData().addAll(result.getData());
            result.getData().clear();
            if(result.getData().size()<1000) {
                ((IGetHotMovieView) this.view).onSucceedGetHotMovie(mGetHotMovieResult);
            } else {
                model.getMovie(this, mCategoryList.getCategory_id(), end+1, end+1000);
            }

        }
    }
}
